package com.utils;

import org.apache.commons.lang3.StringUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.*;

/**
 * 时间工具类
 *      1 String->LocalDate
 *      2 String->LocalDateTime
 *      3 String->Date
 *      4 Long->LocalDateTime
 *
 *      11 Date->LocalDateTime
 *      12 Date->LocalDate
 *      13 LocalDateTime->Date
 *      14 LocalDate->Date
 *
 *      21 LocalDateTime->String
 *      22 LocalDate->String
 *      23 Date->String
 */
public class DateIotUtil {

    public static final String TIME_PATTERN1 = "yyyy-MM-dd";
    public static final String TIME_PATTERN2 = "yyyy-MM-dd HH:mm:ss";
    public static final String TIME_PATTERN3 = "yyyyMMddHHmmss";
    public static final String TIME_PATTERN6 = "HH:mm";

    public static Date parseHHSSToDate(String time) {
        if (StringUtils.isBlank(time)) {
            return new Date();
        }
        try {
            SimpleDateFormat sf = new SimpleDateFormat(TIME_PATTERN2);
            Date date = sf.parse(time);
            return date;
        } catch (ParseException e) {
            return new Date();
        }catch (Exception e) {
            return new Date();
        }
    }

    public static Date parseLocalDateTimeToDate(LocalDateTime time){
        try {
            if(time == null){
                return new Date();
            }
            Date date = Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
            return date;
        } catch (Exception e) {
            return new Date();
        }
    }

    public static LocalDateTime parseDateToLocalDateTime(Date time){
        try {
            if(time == null){
                return LocalDateTime.now();
            }
            LocalDateTime ldt = time.toInstant().atZone( ZoneId.systemDefault()).toLocalDateTime();
            return ldt;
        } catch (Exception e) {
            return LocalDateTime.now();
        }
    }

    public static LocalDate parseDateToLocalDate(Date time){
        try {
            if(time == null){
                return LocalDate.now();
            }
            LocalDateTime ldt = time.toInstant().atZone( ZoneId.systemDefault()).toLocalDateTime();
            return ldt.toLocalDate();
        } catch (Exception e) {
            return LocalDate.now();
        }
    }
    /**
     * 设备入库
     * 新增数据, 格式化时间
     */
    public static Date parseDeviceToDate(String time) {
        if (StringUtils.isBlank(time)) {
            return new Date();
        }
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse(time);
            return date;
        } catch (ParseException e) {
            return new Date();
        }catch (Exception e) {
            return new Date();
        }
    }

    // String->LocalDate
    public static LocalDate parseToLocalDate(String time) {
        if (StringUtils.isBlank(time)) {
            return LocalDate.now();
        }
        try {
            DateTimeFormatter fmt = DateTimeFormatter.ofPattern(TIME_PATTERN1);
            LocalDate date = LocalDate.parse(time, fmt);
            return date;
        } catch (DateTimeParseException ex){
            return LocalDate.now();
        } catch (Exception e) {
            return LocalDate.now();
        }
    }
    // 2 String->LocalDateTime
    public static LocalDateTime parseToLocalDateTime(String time) {
        if (StringUtils.isBlank(time)) {
            return LocalDateTime.now();
        }
        try {
            DateTimeFormatter fmt = DateTimeFormatter.ofPattern(TIME_PATTERN2);
            LocalDateTime date = LocalDateTime.parse(time, fmt);
            return date;
        } catch (DateTimeParseException ex) {
            return LocalDateTime.now();
        } catch (Exception e) {
            return LocalDateTime.now();
        }
    }
    // 3 String->Date
    public static Date parseToDate(String time) {
        if (StringUtils.isBlank(time)) {
            return new Date();
        }
        try {
            // 判断下时间  2022-06-06 14:16:25
            SimpleDateFormat sf = new SimpleDateFormat(TIME_PATTERN1);
            if(time.length() > 10){
                sf = new SimpleDateFormat(TIME_PATTERN2);
            }
            Date date = sf.parse(time);
            return date;
        } catch (ParseException e) {
            return new Date();
        }catch (Exception e) {
            return new Date();

        }
    }
    // 4 Long->LocalDateTime
    public static LocalDateTime parseLongToLocalDateTime(Long time){
        try {
            LocalDateTime ldt =Instant.ofEpochSecond(time).atOffset(ZoneOffset.of("+08:00")).toLocalDateTime();
            return ldt;
        } catch (Exception e) {
            return LocalDateTime.now();
        }
    }
    public static LocalDateTime parseLongToLocalDateTime(Object time){
        try {
            LocalDateTime ldt =Instant.ofEpochSecond(Long.parseLong(time+"")).atOffset(ZoneOffset.of("+08:00")).toLocalDateTime();
            return ldt;
        } catch (Exception e) {
            return LocalDateTime.now();
        }
    }
    // 11 Date->LocalDateTime
    public static LocalDateTime changeToLocalDateTime(Date time){
        try {
            if(time == null){
                return LocalDateTime.now();
            }
            LocalDateTime ldt = time.toInstant().atZone( ZoneId.systemDefault()).toLocalDateTime();
            return ldt;
        } catch (Exception e) {
            return LocalDateTime.now();
        }
    }
    // 12 Date->LocalDate
    public static LocalDate changeToLocalDate(Date time){
        try {
            if(time == null){
                return LocalDate.now();
            }
            LocalDateTime ldt = time.toInstant().atZone( ZoneId.systemDefault()).toLocalDateTime();
            return ldt.toLocalDate();
        } catch (Exception e) {
            return LocalDate.now();
        }
    }
    // 13 LocalDateTime->Date
    public static Date changeToDate(LocalDateTime time){
        try {
            if(time == null){
                return new Date();
            }
            Date date = Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
            return date;
        } catch (Exception e) {
            return new Date();
        }
    }
    //  14 LocalDate->Date
    public static Date changeToDate(LocalDate time){
        try {
            if(time == null){
                return new Date();
            }
            Date date = Date.from(time.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
            return date;
        } catch (Exception e) {
            return new Date();
        }
    }
    // 21 LocalDateTime->String
    public static String formatLocalDateTime(LocalDateTime time){
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(TIME_PATTERN2);
        String formatDate = dtf.format(time);
        return formatDate;
    }
    // 22 LocalDate->String
    public static String formatLocalDate(LocalDate time){
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(TIME_PATTERN1);
        String formatDate = dtf.format(time);
        return formatDate;
    }
    public static String formatLocalDate(LocalDateTime time){
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(TIME_PATTERN2);
        String formatDate = dtf.format(time);
        return formatDate;
    }
    // 23 Date->String
    public static String formatDate(Date date){
        String dateTime = "";
        SimpleDateFormat sdf = new SimpleDateFormat(TIME_PATTERN1);
        try{
            dateTime = sdf.format(date);
        }catch (Exception e){
            dateTime = sdf.format(new Date());
        }
        return dateTime;
    }
    public static String formatDate2(Date date){
        String dateTime="";
        SimpleDateFormat sdf = new SimpleDateFormat(TIME_PATTERN2);
        try{
            dateTime = sdf.format(date);
        }catch (Exception e){
            dateTime = sdf.format(new Date());
        }
        return dateTime;
    }
    public static String formatDate3(Date date){
        String dateTime = "";
        SimpleDateFormat sdf = new SimpleDateFormat(TIME_PATTERN3);
        try{
            dateTime = sdf.format(date);
        }catch (Exception e){
            dateTime = sdf.format(new Date());
        }
        return dateTime;
    }
    // 获取当前小时 2023-01-06T15:00
    public static LocalDateTime getHourStart(){
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime tempTime = LocalDateTime.of(now.getYear(), now.getMonthValue(), now.getDayOfMonth(), now.getHour(),0,0);
        return tempTime;
    }
    //  格式化为HH:ss
    public static String formatHourMinus(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat(TIME_PATTERN6);
        String datetime = sdf.format(date);
        return datetime;
    }
    // 2023-01-06 15:55:00
    public static String getDate5Minus(){
        LocalDateTime now = LocalDateTime.now();
        int minus = now.getMinute();
        if(minus<5){
            minus = 0;
        }else if(minus<10 && minus>=5){
            minus = 5;
        }else if(minus<15 && minus>=10){
            minus = 10;
        }else if(minus<20 && minus>=15){
            minus = 15;
        }else if(minus<25 && minus>=20){
            minus = 20;
        }else if(minus<30 && minus>=25){
            minus = 25;
        }else if(minus<35 && minus>=30){
            minus = 30;
        }else if(minus<40 && minus>=35){
            minus = 35;
        }else if(minus<45 && minus>=40){
            minus = 40;
        }else if(minus<50 && minus>=45){
            minus = 45;
        }else if(minus<55 && minus>=50){
            minus = 50;
        }else if(minus>=55){
            minus = 55;
        }
        String result = LocalDate.now().toString()+" "+now.getHour()+":"+minus+":00";
        return result;
    }
    // 获取当天0点-2023-01-06T00:00
    public static LocalDateTime getDayMin(){
        LocalDateTime todayStart = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);//当天零点
        return todayStart;
    }
    // 获取当天24点-2023-01-06T23:59:59.999999999
    public static LocalDateTime getDayMax(){
        LocalDateTime todayEnd = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);//当天零点
        return todayEnd;
    }
    public static String formatLocalDate2(LocalDateTime time){
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(TIME_PATTERN3);
        String formatDate = dtf.format(time);
        return formatDate;
    }
    public static LocalDateTime parseDoubleToLocalDateTime(Double time){
        long date = time.longValue();
        LocalDateTime ldt =Instant.ofEpochSecond(date).atOffset(ZoneOffset.of("+08:00")).toLocalDateTime();
        return ldt;
    }
}
